/// **************************************************************************************************
/// "Copyright (c) 2007 Sai Panyam, Inc.  All rights reserved."
/// **************************************************************************************************
/// Component Name   :	AcronymModifier, Member of System72.NTextile 
/// Description      :	AcronymModifier wraps all three or more capital letters with an acronym XHTML tag.
/// Author           :	Sai Panyam
/// Creation Date    :	November 09 2007
/// Modified Date    :              
/// Modified By      :
/// **************************************************************************************************
using System.Text.RegularExpressions;
using System72.NTextile.RegexLibrary;

namespace System72.NTextile
{
    /// <summary>
    /// AcronymModifier wraps three or more capital letters with an acronym XHTML tag.
    /// It also provides for setting a title attribute value. 
    /// </summary>
    internal class AcronymModifier:IModifier
    {
        #region Private Variables

        private AttributeCollection m_supportedAttributes;
        private PunctuationCollection m_supportedPunctuations;
        private bool m_success;
        private readonly int m_parseOrder=9;
        private readonly ModifierType m_type;
        
        #endregion

        #region Constructors
        /// <summary>
        /// Constructor of AcronymModifier.
        /// Sets the type of the modifier to Acronym.
        /// </summary>
        public AcronymModifier()
        {
            m_type = ModifierType.Acronym;
        }

        #endregion
        
        #region Public Properties
        /// <summary>
        /// A collection of supported attributes
        /// </summary>
        public AttributeCollection SupportedAttributes
        {
            get
            {
                return m_supportedAttributes;
            }
            set
            {
                m_supportedAttributes = value;
            }
        }
        /// <summary>
        /// A collection of supported punctuations
        /// </summary>
        public PunctuationCollection SupportedPunctuations
        {
            get { return m_supportedPunctuations; }
            set { m_supportedPunctuations = value; }
        }
        /// <summary>
        /// A property to indicate the match result.
        /// </summary>
        public bool Success
        {
            get { return m_success; }
            set { m_success = value; }
        }
        /// <summary>
        /// The order in which to parse this modifier
        /// </summary>
        public int ParseOrder
        {
            get { return m_parseOrder; }
        }
        /// <summary>
        /// The modifier type.
        /// </summary>
        public ModifierType Type
        {
            get { return m_type; }
        }

        #endregion

        #region Public Methods
        /// <summary>
        /// Generates XHTML markup on successful match of three or more capital letters.
        /// </summary>
        /// <param name="contentText">The string to match</param>
        /// <param name="excludedModifiers">A list of modifiers that shouldn't be run against this string</param>
        /// <returns>A valid XHTML acronym mark up</returns>
        public string GetXHTML(string contentText, ModifierCollection excludedModifiers)
        {
            if (!excludedModifiers.ContainsModifier(this))
            {
                AcronymRegex acronymRegex = new AcronymRegex();
                MatchCollection matches = acronymRegex.GetMatches(contentText);
                if(matches!=null && matches.Count>0)
                {
                    foreach(Match match in matches)
                    {
                        //Since there is atleast one match then we can set Success to true
                        m_success = true;
                        string matchText = match.Value;
                        string acronymText = match.Groups[1].Value;
                        string acronymTitle = match.Groups[3].Value;
                        string acronymMarkUp = Util.GetElementMarkUp(acronymText, "acronym", "title=\"" + acronymTitle + "\"");
                        contentText = contentText.Replace(matchText, acronymMarkUp);
                    }
                }
            }
            return contentText;
        }

        #endregion

    }
}
